﻿

prettyPrint();
layui.use(['form', 'layedit'], function () {
    var form = layui.form;
    var $ = layui.jquery;
    var layedit = layui.layedit;

    //评论和留言的编辑器
    var editIndex = layedit.build('remarkEditor', {
        height: 150,
        tool: ['face', '|', 'left', 'center', 'right', '|', 'link'],
    });
    //评论和留言的编辑器的验证
    form.verify({
        content: function (value) {
            value = $("#commentTextArea").val();
            if (value == "") return "自少得有一个字吧";
            layedit.sync(editIndex);
        }
    });

    //监听评论提交
    form.on('submit(formRemark)', function (data) {
        var id = getUrlParms("id");
        var index = layer.load(1);


        var bean = {
            blogId:id,
            content:$("#commentTextArea").val()
        };
        
       axios.post("/personal_blog/comments",bean).then(function(res){
            var data = res.data;
           //模拟评论提交
           setTimeout(function () {
               layer.close(index);
               $("#commentTextArea").val('');
               layer.msg("评论审核中，审核通过后显示", { icon: 1 });
           }, 500);
       })
        return false;
    });
    
    
    $(function(){
    	var id = getUrlParms("id");
    	
    	
    	if(id != null){
    		axios.get("/personal_blog/blogs/"+id).then(function(res){
    			var bean = res.data;
    			$(".article-detail-title").html(bean.title);
    			$("#releaseDate").html("编辑时间："+moment(bean.releaseDate).format('YYYY-MM-DD HH:mm'));
    			$("#newsAuthor").html("作者："+bean.newsAuthor);
    			$("#clickHit").html("查看量："+bean.clickHit);
    			$("#replyHit").html("回复量："+bean.replyHit);
    			$("#typeName").html(bean.typeName);
    			$("#typeName").attr("href","/personal_blog/article?typeId="+bean.typeId+"&typeName="+bean.typeName)
    			$("#title").html(bean.title);
    			
    			$(".article-detail-content").html(bean.content);
    		})

            axios.get("/personal_blog/comments/"+id).then(function (res) {
                var beans = res.data;
                console.log(beans.length)
                if(beans.length>0){
                    for(var i in beans){
                        var j = Math.ceil(Math.random()*20);
                        var html = '<li><div class="comment-parent"><svg class="icon-comment" aria-hidden="true"><use xlink:href="#icon-'+j+'"></use></svg><div class="info"><span class="username">'+beans[i].userIp+'</span><span class="time">'+moment(beans[i].commentDate).format('YYYY-MM-DD HH:mm:ss')+'</span></div><div class="content">' + beans[i].content + '</div></div></li>';
                        $('.blog-comment').append(html);
                    }
                }else{
                	
                	var html = '<li><div class="comment-parent"><div class="info"><span class="username"></span><span class="time"></span></div><div class="content-noReply">暂无</div></div></li>';
                    $('.blog-comment').append(html);
                }
                
               



            })


    	}
    	
    	//文章类别列表
   		axios.get("/personal_blog/blogTypesAll").then(function(res){
   			var data = res.data;
   			var aArr = '';
   			var a = $(".article-category-title");
   			for(var i in data){
   				aArr = $('<a href="/personal_blog/article?typeId='+data[i].id+'&typeName='+data[i].typeName+'">'+data[i].typeName+'</a>');
   				a.after(aArr);
   				a = aArr;
   			}
   		})
    	
    	
    	
    	
    })
    
    

    function getUrlParms(para){
        var search=location.search; //页面URL的查询部分字符串
        var arrPara=new Array(); //参数数组。数组单项为包含参数名和参数值的字符串，如“para=value”
        var arrVal=new Array(); //参数值数组。用于存储查找到的参数值

        if(search!=""){
            var index=0;
            search=search.substr(1); //去除开头的“?”
            arrPara=search.split("&");

            for(i in arrPara){
                var paraPre=para+"="; //参数前缀。即参数名+“=”，如“para=”
                if(arrPara[i].indexOf(paraPre)==0&& paraPre.length<arrPara[i].length){
                    arrVal[index]=decodeURI(arrPara[i].substr(paraPre.length)); //顺带URI解码避免出现乱码
                    index++;
                }
            }
        }

        if(arrVal.length==1){
            return arrVal[0];
        }else if(arrVal.length==0){
            return null;
        }else{
            return arrVal;
        }
    }
    
    
});